package com.hylanda.pb.controller;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hylanda.pb.entity.TableSql;
import com.hylanda.pb.entity.dict.EnableType;
import com.hylanda.pb.model.TableSqlQo;
import com.hylanda.pb.service.TableSqlService;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2018年2月22日 下午6:04:04 
 * note
 */
@Controller
@RequestMapping("/tablesql")
public class TableSqlController {
	private static Logger logger = LoggerFactory.getLogger(TableSqlController.class);
	private static final String prefix = "pb/";
	@Autowired
	private TableSqlService tableSqlService;

	@RequestMapping("/index")
	public String index() throws Exception {
		return prefix + "tablesql/index";
	}

	@RequestMapping("/new")
	public String create(ModelMap model, TableSql tableSql) {
		model.addAttribute("enableTypes",EnableType.getAllEnabletype());
		return prefix + "tablesql/new";
	}
	@RequestMapping(value = "/edit/{id}")
	public String update(ModelMap model, @PathVariable Long id) {
		TableSql tableSql = tableSqlService.findById(id);
		model.addAttribute("enableTypes",EnableType.getAllEnabletype());
		model.addAttribute("tableSql",tableSql);
		return prefix + "tablesql/edit";
	}
	
	@RequestMapping(value = "/{id}")
	public String show(ModelMap model, @PathVariable Long id) {
		TableSql tableSql = tableSqlService.findById(id);
		model.addAttribute("tableSql",tableSql);
		model.addAttribute("enableTypes",EnableType.getAllEnabletype());
		return prefix + "tablesql/show";
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Page<TableSql> getList(TableSqlQo tableSqlQo) {
		return tableSqlService.findPage(tableSqlQo);
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request,TableSql tableSql) throws Exception {
		String OldfileName=tableSqlService.findByName(tableSql.getTableName());
		if(OldfileName==null){
			tableSql=tableSqlService.create(tableSql);
			logger.info("新增->ID=" + tableSql.getId());
			return "1";
		}else{
			return "表sql已存在，不能重复上传";
		}
	}

	@RequestMapping(method = RequestMethod.POST, value = "/update")
	@ResponseBody
	public String update(TableSql tableSql) throws Exception {
		tableSqlService.update(tableSql);
		logger.info("修改->ID=" + tableSql.getId());
		return "1";
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String delete(@PathVariable Long id) throws Exception {
		int rownum=tableSqlService.delete(id);
		if(rownum==1){
			logger.info("删除->ID=" + id);
			return "1";
		}else{
			return "删除操作失败";
		}
	}
}
